from OpenGL.GL import *
import pyglet

from common import *
from common.util import file_path
from common.terrain import Terrain
from game.core.gamemanager import GameManager
from game.ui.controlbar import ControlBar
from game.ui.statusbar import StatusBar
from game.ui.mainscreen import MainScreen

from common.units.nanwarrior import *

class GamePlay(object):
	"""
	This class encapsulates the gameplay interface.
	"""
	
	def __init__(self):
		self.win_size = [GameManager.main_window.win.width,
						GameManager.main_window.win.height]
		self.heights = [200, GameManager.main_window.win.height - 250, 50]
		self.stretches = [None] * 3
		self.__set_stretches()
		
		self.controlbar = ControlBar()
		self.statusbar = StatusBar() 
		viewport = [0, self.heights[0], self.win_size[X], self.heights[1]]
		self.mainscreen = MainScreen(viewport)
		
		self.move_left = False
		self.move_right = False
		self.move_up = False
		self.move_down = False
	
	def __set_stretches(self):
		total = float(self.win_size[Y]) 
		
		self.stretches[0] = total / self.heights[0]
		self.stretches[1] = total / self.heights[1]
		self.stretches[2] = total / self.heights[2]
		
	def __push_matrixes(self):
		glMatrixMode(GL_PROJECTION)
		glPushMatrix()
		glMatrixMode(GL_MODELVIEW)
		glPushMatrix()
		
	def __pop_matrixes(self):
		glMatrixMode(GL_PROJECTION)
		glPopMatrix()
		glMatrixMode(GL_MODELVIEW)
		glPopMatrix()
		
	def initialize(self):
		GameManager.map.load(file_path('../data/maps/map01.cgm'))
		GameManager.lighting.addLight(GL_LIGHT0)
		GameManager.lighting.setLight(GL_LIGHT0, [1.0, 1.0, 1.0, 0],
											[0.4, 0.7, 0.9, 1],
											[0.2, 0.9, 0.6, 1])
		
	def tick(self, dt):
		if self.move_left:
			GameManager.camera.position += 0.2 * GameManager.camera.leftVector
		elif self.move_right:
			GameManager.camera.position -= 0.2 * GameManager.camera.leftVector
		
		if self.move_up:
			GameManager.camera.position -= 0.2 * GameManager.camera.upVector
		elif self.move_down:
			GameManager.camera.position += 0.2 * GameManager.camera.upVector
			
		for unit in GameManager.map.units:
			unit.tick(dt)
		
	def animate(self):
		for unit in GameManager.map.units:
			unit.animate()
	
	def on_mouse_motion(self, x, y, dx, dy):
		if x == 0:
			self.move_left = True
			self.move_right = False
		elif x == self.win_size[X] - 1:
			self.move_right = True
			self.move_left = False
		else:
			self.move_left = False
			self.move_right = False
		
		if y == 1:
			self.move_up = True
			self.move_down = False
		elif y == self.win_size[Y]:
			self.move_down = True
			self.move_up = False
		else:
			self.move_up = False
			self.move_down = False
			
		if y <= self.heights[0]:
			self.controlbar.on_mouse_motion(x, y, dx, dy)
		elif y <= self.heights[0] + self.heights[1]:
			pass
			#self.mainscreen.mouse_press(x, y, button)
		else:
			pass
	
	def on_mouse_press(self, x, y, button, modifiers):
		if y <= self.heights[0]:
			self.controlbar.on_mouse_press(x, y, button, modifiers)
		elif y <= self.heights[0] + self.heights[1]:
			self.mainscreen.mouse_press(x, y, button)
		else:
			pass

	def on_mouse_release(self, x, y, button, modifiers):
		pass
	
	def on_draw(self):
		GameManager.main_window.win.clear()
		
		# Renders the control bar
		self.__push_matrixes()
		glViewport(0, 0, self.win_size[X], self.heights[0])
		glScale(1, self.stretches[0], 1)
		
		self.controlbar.render()
		
		self.__pop_matrixes()
		
		# Renders the game screen
		self.__push_matrixes()
		
		glViewport(0, self.heights[0], self.win_size[X], self.heights[1])
		#glScale(1, self.stretches[1], 1)
		
		glEnable(GL_DEPTH_TEST)
		self.mainscreen.render()
		glDisable(GL_DEPTH_TEST)
		
		self.__pop_matrixes()
		
		# Renders the top bar
		self.__push_matrixes()
		
		glViewport(0, self.heights[0] + self.heights[1],
					self.win_size[X], self.heights[2])
		glScale(1, self.stretches[2], 1)
		
		self.statusbar.render()
		
		self.__pop_matrixes()